import funcUtil from "./func.util"
import n from "../packages/60039"
import ObservableArray, { createArray } from "../observer/observable.array"
import { groupData } from "./Grouper"
export default class Group {
  startIndex: number
  index: number
  grouper: ObservableArray<THREE.Vector3>
  endIndex: number
  data: groupData
  constructor(e: number, t: ObservableArray<THREE.Vector3>, i: number, n: number, s: groupData) {
    this.index = e
    this.grouper = t
    this.startIndex = i
    this.endIndex = n
    this.data = s
  }
  *[Symbol.iterator]() {
    for (let e = 0; e < this.count; ++e) yield this.get(e)
  }
  get(e: number) {
    if (!this.has(e)) throw RangeError(`Out of range error ${e} / ${this.count - 1}`)
    return this.grouper.get(this.startIndex + e)
  }
  has(e: number) {
    return e >= 0 && this.startIndex + e <= this.endIndex
  }
  get count() {
    return this.endIndex - this.startIndex + 1
  }
  get info() {
    return this.data
  }
  get isClosed() {
    const e = this.grouper.get(this.startIndex),
      t = this.grouper.get(this.endIndex)
    return this.count > 2 && e.distanceTo(t) <= n.NZ
  }
  get length() {
    let e = 0,
      t = null
    for (let i = 0; i < this.count; i++) t && (e += this.get(i).distanceTo(t)), (t = this.get(i))
    return e
  }
  hasLength() {
    const e = this.grouper.get(this.startIndex),
      t = this.grouper.get(this.endIndex)
    return e && t && (this.isClosed || e.distanceTo(t) > n.NZ)
  }
  clone() {
    const t = createArray([])
    for (let e = 0; e < this.count; e++) t.push(this.get(e))
    return new Group(this.index, t, 0, t.length - 1, Object.assign({}, this.data))
  }
  describe(e = this.endIndex) {
    return `GroupSegment${this.index}/${this.startIndex}/${e}}`
  }
  equals(e) {
    if (this.count !== e.count || this.index !== e.index) return !1
    if (funcUtil.deepDiffers(this.info, e.info)) return !1
    for (let t = 0; t < this.count; ++t) if (!this.get(t).equals(e.get(t))) return !1
    return !0
  }
}
